package dimension.starry.block.machine.simple.furnace;

import dimension.starry.StarryDimension;
import dimension.starry.block.machine.simple.SimpleMachineRegister;
import dimension.starry.block.typeInterface.MachineTileEntity;
import dimension.starry.constants.ItemNbtTag;
import dimension.starry.item.ItemRegisterManager;
import net.minecraft.block.BlockState;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.screen.ScreenHandler;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import org.jetbrains.annotations.Nullable;

/**
 * @author 150149
 */
public class SimpleFurnaceTileEntity extends MachineTileEntity {

    private static final Text TITLE = Text.translatable("block." + StarryDimension.MOD_ID + ".simple_furnace");

    public SimpleFurnaceTileEntity(BlockPos blockPos, BlockState blockState) {
        super(SimpleMachineRegister.SIMPLE_FURNACE.getBlockEntityType(), blockPos, blockState, 4, TITLE, 800);
    }

    @Nullable
    private static Item getOutputItem(Item in) {
        if (in.equals(ItemRegisterManager.BROKEN_TIN.getItem())) {
            return ItemRegisterManager.TIN.getItem();
        } else if (in.equals(Items.RAW_COPPER)) {
            return Items.COPPER_INGOT;
        }
        return null;
    }

    @Override
    public ScreenHandler createMenu(int sycID, PlayerInventory inventory, PlayerEntity player) {
        return new SimpleFurnaceContainer(
            SimpleMachineRegister.SIMPLE_FURNACE.getScreenHandlerType(), sycID, inventory, getPos());
    }

    @Override
    public void tick(World world, BlockPos pos, BlockState blockState, MachineTileEntity blockEntity) {
        if (world == null || world.isClient()) {
            return;
        }
        ItemStack potItemStack = this.inventory.getStack(0);
        ItemStack inputItemStack = this.inventory.getStack(1);
        ItemStack fuelItemStack = this.inventory.getStack(2);

        if (potItemStack.isEmpty() || inputItemStack.isEmpty()) {
            this.setMachineStatus(false);
            this.resetProgress();
        } else if (!potItemStack.getItem().equals(ItemRegisterManager.STONE_POT.getItem())) {
            this.setMachineStatus(false);
            this.resetProgress();
        } else if (getOutputItem(inputItemStack.getItem()) == null) {
            this.setMachineStatus(false);
            this.resetProgress();
        } else if (this.getProgress() >= this.getConsumeTime()) {
            ItemStack itemStack = new ItemStack(ItemRegisterManager.LAVA_STONE_POT.getItem());
            NbtCompound compoundNBT = new NbtCompound();
            compoundNBT.putString(
                ItemNbtTag.STONE_POT_OUT, getOutputItem(inputItemStack.getItem()).getTranslationKey());
            itemStack.setNbt(compoundNBT);
            if (this.inventory.canInsert(itemStack)) {
                inputItemStack.setCount(inputItemStack.getCount() - 1);
                potItemStack.setCount(potItemStack.getCount() - 1);
                this.resetProgress();
                this.inventory.addStack(itemStack);
            }
        } else if (!this.consumeFuel() && !fuelItemStack.isEmpty() && getFuelAddCount(fuelItemStack.getItem()) != 0) {
            fuelItemStack.setCount(fuelItemStack.getCount() - 1);
            this.addFuel(getFuelAddCount(fuelItemStack.getItem()));
            this.consumeFuel();
            this.setMachineStatus(true);
        }
    }

}
